

package org.apache.hadoop.mapreduce.lib.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.lib.db.DBInputFormat.NullDBWritable;

public class DBConfiguration {
    public static final String DRIVER_CLASS_PROPERTY = "mapred.jdbc.driver.class";
    public static final String URL_PROPERTY = "mapred.jdbc.url";
    public static final String USERNAME_PROPERTY = "mapred.jdbc.username";
    public static final String PASSWORD_PROPERTY = "mapred.jdbc.password";
    public static final String INPUT_TABLE_NAME_PROPERTY = "mapred.jdbc.input.table.name";
    public static final String INPUT_FIELD_NAMES_PROPERTY = "mapred.jdbc.input.field.names";
    public static final String INPUT_CONDITIONS_PROPERTY = "mapred.jdbc.input.conditions";
    public static final String INPUT_ORDER_BY_PROPERTY = "mapred.jdbc.input.orderby";
    public static final String INPUT_QUERY = "mapred.jdbc.input.query";
    public static final String INPUT_COUNT_QUERY = "mapred.jdbc.input.count.query";
    public static final String INPUT_BOUNDING_QUERY = "mapred.jdbc.input.bounding.query";
    public static final String INPUT_CLASS_PROPERTY = "mapred.jdbc.input.class";
    public static final String OUTPUT_TABLE_NAME_PROPERTY = "mapred.jdbc.output.table.name";
    public static final String OUTPUT_FIELD_NAMES_PROPERTY = "mapred.jdbc.output.field.names";
    public static final String OUTPUT_FIELD_COUNT_PROPERTY = "mapred.jdbc.output.field.count";
    private Configuration conf;

    public static void configureDB(Configuration conf, String driverClass, String dbUrl, String userName, String passwd) {
        conf.set("mapred.jdbc.driver.class", driverClass);
        conf.set("mapred.jdbc.url", dbUrl);
        if (userName != null) {
            conf.set("mapred.jdbc.username", userName);
        }

        if (passwd != null) {
            conf.set("mapred.jdbc.password", passwd);
        }

    }

    public static void configureDB(Configuration job, String driverClass, String dbUrl) {
        configureDB(job, driverClass, dbUrl, (String) null, (String) null);
    }

    public DBConfiguration(Configuration job) {
        this.conf = job;
    }

    public Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(this.conf.get("mapred.jdbc.driver.class"));
        return this.conf.get("mapred.jdbc.username") == null ? DriverManager.getConnection(this.conf.get("mapred.jdbc.url")) : DriverManager.getConnection(this.conf.get("mapred.jdbc.url"), this.conf.get("mapred.jdbc.username"), this.conf.get("mapred.jdbc.password"));
    }

    public Configuration getConf() {
        return this.conf;
    }

    public String getInputTableName() {
        return this.conf.get("mapred.jdbc.input.table.name");
    }

    public void setInputTableName(String tableName) {
        this.conf.set("mapred.jdbc.input.table.name", tableName);
    }

    public String[] getInputFieldNames() {
        return this.conf.getStrings("mapred.jdbc.input.field.names");
    }

    public void setInputFieldNames(String... fieldNames) {
        this.conf.setStrings("mapred.jdbc.input.field.names", fieldNames);
    }

    public String getInputConditions() {
        return this.conf.get("mapred.jdbc.input.conditions");
    }

    public void setInputConditions(String conditions) {
        if (conditions != null && conditions.length() > 0) {
            this.conf.set("mapred.jdbc.input.conditions", conditions);
        }

    }

    public String getInputOrderBy() {
        return this.conf.get("mapred.jdbc.input.orderby");
    }

    public void setInputOrderBy(String orderby) {
        if (orderby != null && orderby.length() > 0) {
            this.conf.set("mapred.jdbc.input.orderby", orderby);
        }

    }

    public String getInputQuery() {
        return this.conf.get("mapred.jdbc.input.query");
    }

    public void setInputQuery(String query) {
        if (query != null && query.length() > 0) {
            this.conf.set("mapred.jdbc.input.query", query);
        }

    }

    public String getInputCountQuery() {
        return this.conf.get("mapred.jdbc.input.count.query");
    }

    public void setInputCountQuery(String query) {
        if (query != null && query.length() > 0) {
            this.conf.set("mapred.jdbc.input.count.query", query);
        }

    }

    public void setInputBoundingQuery(String query) {
        if (query != null && query.length() > 0) {
            this.conf.set("mapred.jdbc.input.bounding.query", query);
        }

    }

    public String getInputBoundingQuery() {
        return this.conf.get("mapred.jdbc.input.bounding.query");
    }

    public Class<?> getInputClass() {
        return this.conf.getClass("mapred.jdbc.input.class", NullDBWritable.class);
    }

    public void setInputClass(Class<? extends DBWritable> inputClass) {
        this.conf.setClass("mapred.jdbc.input.class", inputClass, DBWritable.class);
    }

    public String getOutputTableName() {
        return this.conf.get("mapred.jdbc.output.table.name");
    }

    public void setOutputTableName(String tableName) {
        this.conf.set("mapred.jdbc.output.table.name", tableName);
    }

    public String[] getOutputFieldNames() {
        return this.conf.getStrings("mapred.jdbc.output.field.names");
    }

    public void setOutputFieldNames(String... fieldNames) {
        this.conf.setStrings("mapred.jdbc.output.field.names", fieldNames);
    }

    public void setOutputFieldCount(int fieldCount) {
        this.conf.setInt("mapred.jdbc.output.field.count", fieldCount);
    }

    public int getOutputFieldCount() {
        return this.conf.getInt("mapred.jdbc.output.field.count", 0);
    }
}
